<template>
  <!-- 添加或修改工序对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" class="detail-tab" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="工序名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入工序名称" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="生产产品名称" prop="goodsName">
                <el-input v-model="form.goodsName" placeholder="请输入生产产品名称" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="生产产品型号" prop="goodsModel">
                <el-input v-model="form.goodsModel" placeholder="请输入生产产品型号" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="24">
              <el-form-item label="图纸号" prop="shuoming">
                <el-input v-model="form.shuoming" type="textarea" placeholder="请输入图纸号" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="12">
               <el-form-item label="适用车间" prop="deptName">
                <el-select v-model="form.deptName" placeholder="请选择适用车间" style="width: 100%"
                  @change="handshopChange">
                  <el-option v-for="(item, index) in workshopOptions" :key="index" :label="item.label"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="标准工时" prop="workHour">
                <el-input-number v-model="form.workHour" placeholder="请输入标准工时" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addAction, getById, updateAction, getAction } from '@/api/manage';
import moment from 'moment';

export default {
  name: "LnGongxuFormAll",
  dicts: ['shebei_list'],
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "批量修改工序信息",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      queryParams: {},
      // 表单校验
      rules: {

      },
      url: {
        getById: '/lnGongxu/lnGongxu/',
        editAll: '/lnGongxu/lnGongxu/editAll',
        selectMaxCode: '/lnGongxu/lnGongxu/selectMaxCode',
         userTreeSelect: '/system/dept/userTreeSelect'
      },
       workshopOptions: [

      ],
    };
  },
  created() {
     this.getUser();
  },
  methods: {
      handshopChange(e) {
      // 查找匹配的选项，不仅匹配value，也匹配id
      const selected = this.workshopOptions.find(item => item.value === e || item.id === e);
      if (selected) {
        // 设置表单的部门ID和名称
        this.form.deptId = selected.id;
        this.form.deptName = selected.label;
      }
    },
    getUser() {
      let that = this;
      getAction(that.url.userTreeSelect, { parentId: '57d779853f0f4d14b03d51db30099456' }).then(res => {
        that.workshopOptions = res.data;
      })
    },
    selectShebei(value) {
      let that = this;
      that.$nextTick(() => {
        let arr = that.$refs.myselectShebei;
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].currentValue == value) {
            //that.form.kehuName = arr[i].label;
          }
        }
      })
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ids: null,
        code: null,
        name: null,
        money: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        shuoming: null,
        file: null,
        photo: null,
        status: "0",
        deptId: null,
        deptName: null,
        workHour: null,
        goodsName:null,
        goodsModel:null,
        /*  haveGongyi: null,
          isZhijian: null,
          readyDate: null,
          prodDate: null,
          endDate: null,
          zhijianDate: null*/
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工序";
      this.selectMaxCode();
    },
    selectMaxCode() {
      this.loading = true;
      getAction(this.url.selectMaxCode).then(response => {
        let code = parseInt(response.data.code);
        if (code != null && code != '' && code != 'undefined') {
          this.form.code = String(code + 1).padStart(5, '0');
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(ids) {
      this.reset();
      this.form.ids = ids;
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ids != null) {
            if(this.form.workHour == false) {
              this.form.workHour = null;
            }
            updateAction(this.url.editAll, this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('ok');
            });
          }
        }
      });
    },
  }
};
</script>
